<?php
class CategoryAction extends CommonAction {

    public function index() {
        C('TOKEN_ON',false);
        $nav = M('InfoNav');
        $attr = M('InfoAttr');
        $info = M('Info');
        $navName = $nav->alias('i1')
                       ->field(array('i1.name as cname,i1.id as cid,i2.name as pname,i2.id as pid'))
                       ->join('xinxi_info_nav as i2 ON i1.pid=i2.id')
                       ->where('i1.id='.$_GET['id'])
                       ->find();

        $childNav = $nav->field(array('id', 'name'))
                        ->where('pid='.$_GET['id'].' AND is_active=1')
                        ->order('sort ASC')
                        ->select();

        //循环信息
        $pid = $nav->where('id='.$_GET['id'])->getField('pid');

        //筛选功能
        $qtemp = $_GET;
        unset($qtemp['_URL_']);     //去除tp的预定义变量
        unset($qtemp['id']);     //去除id变量
        unset($qtemp['p']);        //去除分页变量
        $likeArr = array();
        foreach($qtemp as $key=>$value) {
            $like = "%$key/$value%";
            $likeArr[] = $like;
        }
        if($pid == 0) {          //当前为父导航
            $childId = '';
            $temp = $nav->field('id')->where('pid='.$_GET['id'])->select();
            foreach($temp as $key=>$value) {
                $childId .= $value['id'].',';
            }
            $childId = substr($childId, 0, -1);         //列出父类导航下所有子导航
            $map['cid'] = array('in', $childId);
            $this->_listInfo($qtemp, $likeArr, $info, $map);
        } else {
            $id = $_GET['id'];
            $map['cid'] = array('in', $id);
            $this->_listInfo($qtemp, $likeArr, $info, $map);
        }

        //列举属性
        //判断是子类导航还是父类导航
        if(empty($navName['pname'])) {           //父类导航
            $navAttr = $nav->where('id='.$_GET['id'])->getField('attr');
        } else {
            $navAttr = $nav->where('id='.$navName['pid'])->getField('attr');
        }

        $map['id'] = array('in', $navAttr);
        $map['is_select'] = 1;
        $myAttr = array();
        $temp = $attr->field(array('nickname', 'name', 'details'))->where($map)->select();
        foreach($temp as $key=>$value) {         //把属性内容做成数组
            $value['details'] = explode(',', $value['details']);
            $myAttr[] = $value;
        }


        $this->assign(array(
           'navname'=>$navName,
           'childnav'=>$childNav,
           'myattr'=>$myAttr,
        ));
        $this->display();
    }

    //列出信息与筛选信息并自动删除过期信息以及判断置顶时间是否已经过期
    private function _listInfo($qtemp , $likeArr, $info, $map) {
        $field = array('id', 'title', 'addtime', 'keep', 'stick', 'stick_days', 'stick_time');

        if(count($qtemp) != 0) $map['attr'] = array('like', $likeArr, 'AND');       //存在like就筛选
        $count = $info->where($map)->count();
        parent::page($count, getSetting('info_page'));
        $allInfo = $info->field($field)
                        ->where($map)
                        ->order('stick DESC,addTime DESC')
                        ->limit($this->limit)
                        ->select();

        $oneDay = 3600*24;
        foreach($allInfo as $key=>&$value) {           //自动删除过期信息
            switch($value['keep']) {
                case '一年':
                    $oneYear = 3600*24*365;
                    $shut = $oneYear+$value['addtime'];
                    $leave = -floor(time() - $shut);     //向下取整时间
                    if (floor($leave / $oneDay) <= 0) $info->where('id='.$value['id'])->delete();
                    break;
                case '一月':
                    $oneMonth = 3600*24*30;
                    $shut = $oneMonth+$value['addtime'];
                    $leave = -floor(time() - $shut);
                    if (floor($leave / $oneDay) <= 0) $info->where('id='.$value['id'])->delete();
                    break;
                case '一周':
                    $oneWeek = 3600*24*7;
                    $shut = $oneWeek+$value['addtime'];
                    $leave = -floor(time() - $shut);
                    if (floor($leave / $oneDay) <= 0) $info->where('id='.$value['id'])->delete();
                    break;
            }
            //判断置顶是否过期
            if(($value['stick_time'] + $value['stick_days']) - time() < 0) {
                $data['stick'] = 0;
                $data['stick_time'] = '';
                $data['stick_days'] = '';
                $info->where('id='.$value['id'])->save($data);
            }

        }

        $this->assign(array(
            'allinfo'=>$allInfo,
            'count'=>$count
        ));

    }


}

?>